import java.util.Stack;

/*
 * @lc app=leetcode.cn id=946 lang=java
 *
 * [946] 验证栈序列
 */

// @lc code=start
class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> st = new Stack<>();
        int j = 0;
        for(int num : pushed){
            st.push(num);
            while (j < popped.length && !st.isEmpty() && st.peek() == popped[j]) {
                st.pop();
                j++;
            }
        }
        return j==popped.length;
    }
}
// @lc code=end

